package com.abc.scholarship.domain.entity;

import com.abc.scholarship.common.enums.RoleEnum;
import com.abc.scholarship.common.enums.SexEnum;
import com.abc.scholarship.domain.dto.StudentImportDto;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import java.time.LocalDateTime;
import com.baomidou.mybatisplus.annotation.TableField;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * <p>
 * 用户表
 * </p>
 *
 * @author LiJunXi
 * @since 2024-07-02
 */
@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false)
@TableName("tb_user")
public class User implements Serializable, UserDetails {

    private static final long serialVersionUID = 1L;

    /**
     * 用户ID
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 用户名
     */
    @TableField("u_name")
    private String uName;


    /**
     * 头像
     */
    @TableField("avatar")
    private String avatar;

    /**
     * 学号/工号/账号
     */
    @TableField("u_account")
    private String uAccount;

    /**
     * 密码
     */
    @TableField(value = "u_password")
    private String uPassword;

    /**
     * 创建时间
     */
    @TableField("create_time")
    private LocalDateTime createTime;

    /**
     * 修改时间
     */
    @TableField("update_time")
    private LocalDateTime updateTime;

    /**
     * 状态（1：正常，2：禁用）
     */
    @TableField("status")
    private Integer status;

    /**
     * 性别（1：男，2：女）
     */
    @TableField("sex")
    private Integer sex;

    @TableField("role_id")
    private Integer roleId;

    public User(String uName, Integer sex, String uAccount, String avatar, String uPassword,
                Integer roleId) {
        this.uName = uName;
        this.sex = sex;
        this.uAccount = uAccount;
        this.avatar = avatar;
        this.uPassword = uPassword;
        this.roleId = roleId;
    }


    public static User buildFromExcel(StudentImportDto item, String uPassword) {
        return new User(item.getUName(), SexEnum.getType(item.getSex()), item.getUAccount(),
                "https://img.picui.cn/free/2024/07/03/6684bc19099b1.png",
                uPassword, RoleEnum.STUDENT.getRoleId());
    }

    /**
     * 权限列表
     */
    @TableField(exist = false)
    Collection<? extends GrantedAuthority> authorities;

    /**
     * 查询用户权限列表
     */
    @TableField(exist = false)
    private List<Menu> menuList;

    @Override
    public String getPassword() {
        return this.uPassword;
    }

    @Override
    public String getUsername() {
        return this.uAccount;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
